package com.base.chat.ac;

import android.content.Intent;
import android.os.Build;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.content.ContextCompat;
import android.text.TextUtils;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.base.chat.R;
import com.base.chat.databinding.ActivityFriendMessgeListBinding;
import com.base.chat.fragment.MessageListFragment;
import com.base.chat.fragment.MyFriendListFragment;
import com.base.commonlib.base.CBaseMvpActivity;
import com.base.commonlib.bean.StatEntity;
import com.base.commonlib.event.StudyEvent;
import com.base.commonlib.utils.RxBus2;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import me.majiajie.pagerbottomtabstrip.NavigationController;
import me.majiajie.pagerbottomtabstrip.listener.OnTabItemSelectedListener;
import me.yokeyword.fragmentation.SupportFragment;


@Route(path = "/chat/friendandmessage")
public class FriendAndMessageActivity extends CBaseMvpActivity {
    protected CompositeDisposable mCompositeDisposable = new CompositeDisposable();
    private ActivityFriendMessgeListBinding mBinding;
    private List<SupportFragment> mFragments;
    private SupportFragment mCurrentFragment;

    @Override
    protected int getLayoutResource() {
        return R.layout.activity_friend_messge_list;

    }

    @Override
    protected void init() {
        try {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                Window window = getWindow();
                window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
                window.setStatusBarColor(getResources().getColor(R.color.color_4486FF));
                //底部导航栏
                //window.setNavigationBarColor(activity.getResources().getColor(colorResId));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        mBinding =  (ActivityFriendMessgeListBinding)mRootBinding;
    }

    @Override
    protected void start() {
        initFragment();
        initBottomTab();
    }

    private void initFragment() {
        mFragments = new ArrayList<>();
        mFragments.add(new MessageListFragment());
        mFragments.add(new MyFriendListFragment());
        mCurrentFragment = mFragments.get(0);
        //默认选中第一个
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        transaction.add(R.id.recycler, mFragments.get(0), mFragments.get(0).getClass().getName());
        transaction.commitAllowingStateLoss();
    }


    private void initBottomTab() {
        final NavigationController navigationController = mBinding.pagerBottomTab.material()
                .addItem(R.mipmap.icon_message, R.mipmap.icon_message_checked, "消息")
                .addItem(R.mipmap.icon_person, R.mipmap.icon_person_checked,"朋友")
                .setDefaultColor(ContextCompat.getColor(mContext, R.color.textColorVice))
                .build();
        //底部按钮的点击事件监听
        navigationController.addTabItemSelectedListener(new OnTabItemSelectedListener() {
            @Override
            public void onSelected(int index, int old) {
                FragmentManager manager = getSupportFragmentManager();
                FragmentTransaction transaction = manager.beginTransaction();
                if (manager.findFragmentByTag(mFragments.get(index).getClass().getName()) != null) {
                    transaction.show(mFragments.get(index));
                } else {
                    transaction.add(R.id.recycler, mFragments.get(index), mFragments.get(index).getClass().getName());
                }
                transaction.hide(mCurrentFragment);
                mCurrentFragment = mFragments.get(index);
//                transaction.replace(R.id.recycler, mFragments.get(index));
                transaction.commitAllowingStateLoss();
            }

            @Override
            public void onRepeat(int index) {
            }
        });

        Disposable disposable = RxBus2.getInstance().doSubscribe(StatEntity.class, new Consumer<StatEntity>() {
            @Override
            public void accept(StatEntity bean) throws Exception {
               if (bean.getState()==1){
                   setLightStatusBar(FriendAndMessageActivity.this, false);
                   try {
                       if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                           Window window = getWindow();
                           window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
                           window.setStatusBarColor(getResources().getColor(R.color.color_4486FF));
                           //底部导航栏
                           //window.setNavigationBarColor(activity.getResources().getColor(colorResId));
                       }
                   } catch (Exception e) {
                       e.printStackTrace();
                   }
               }else {
                   setLightStatusBar(FriendAndMessageActivity.this, true);
                   try {
                       if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                           Window window = getWindow();
                           window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
                           window.setStatusBarColor(getResources().getColor(R.color.color_white));
                           //底部导航栏
                           //window.setNavigationBarColor(activity.getResources().getColor(colorResId));
                       }
                   } catch (Exception e) {
                       e.printStackTrace();
                   }
               }
            }
        });
        RxBus2.getInstance().addSubscription(this, disposable);

        Disposable disposable1 = RxBus2.getInstance().doSubscribe(StudyEvent.class, new Consumer<StudyEvent>() {
            @Override
            public void accept(StudyEvent bean) throws Exception {
                navigationController.setSelect(bean.getSelectCurrent());
            }
        });
        RxBus2.getInstance().addSubscription(this, disposable1);
    }




    @Override
    protected void onDestroy() {
        super.onDestroy();
        mCompositeDisposable.dispose();
    }

    @Override
    protected void onResume() {
        super.onResume();
    }
}
